<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="Do what you say,say what you do!">
<meta property="og:type" content="website">
<meta property="og:title" content="EasyBlog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="EasyBlog">
<meta property="og:description" content="Do what you say,say what you do!">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="EasyBlog">
<meta name="twitter:description" content="Do what you say,say what you do!">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/"/>





  <title> EasyBlog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?07a6d49ae67fe991e0ce3c2f3955e0f9";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-left 
   page-home 
 ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">EasyBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/06/05/Laravel-Collection常用方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/06/05/Laravel-Collection常用方法/" itemprop="url">
                  Laravel Collection常用方法
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-06-05T20:17:15+08:00">
                2017-06-05
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/06/05/Laravel-Collection常用方法/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/06/05/Laravel-Collection常用方法/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <p>  自从<code>Laravel 5.3</code>开始，关于<code>Eloquent ORM</code>操作的返回值变成了<code>Collection</code>，不再是数组，意味着我们对<code>Collection</code>的操作将会变得越来越多，下面总结了一些<code>Collection</code>的常用方法，方便以后使用。</p>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p><code>Illuminate\Support\Collection</code>类提供了一个流畅、便利的封装来操控数组数据。</p>
<h2 id="创建集合"><a href="#创建集合" class="headerlink" title="创建集合"></a>创建集合</h2><p>如上所述，<code>collect()</code> 辅助函数会利用传入的数组生成一个新的 <code>Illuminate\Support\Collection</code> 实例。所以要创建一个集合就这么简单：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$newCollection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</div></pre></td></tr></table></figure>
<p><code>Collection</code> 类支持链式调用，一般来说，每一个 <code>Collection</code> 方法会返回一个全新的 <code>Collection</code> 实例，你可以放心地进行链接调用。</p>
<h2 id="Eloquent-ORM-Collection"><a href="#Eloquent-ORM-Collection" class="headerlink" title="Eloquent ORM Collection"></a>Eloquent ORM Collection</h2><blockquote>
<p>默认 Eloquent 模型的查询结果总是以 <code>Collection</code> 实例返回。</p>
</blockquote>
<p>进入tinker,我们可以用factory方法在<code>users table</code>中生成10条数据。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">factory(App\User::class,<span class="number">10</span>)-&gt;create();</div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</div><div class="line"> * Get a list of users from the users table</div><div class="line"> */</span></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getUsers</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    $users = User::all();</div><div class="line">    dd($users);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/06/05/Laravel-Collection常用方法/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/23/Laravel如何连接多个数据库？/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/23/Laravel如何连接多个数据库？/" itemprop="url">
                  Laravel如何连接多个数据库？
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-23T14:13:05+08:00">
                2017-05-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/23/Laravel如何连接多个数据库？/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/23/Laravel如何连接多个数据库？/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <p>最近在学习使用PHPUnit写测试，在给laravel项目做测试时，因为项目已经有一个数据库了，并且其中也有不少数据，为了防止测试数据污染现有数据。于是想到针对测试可以建立一个相同的数据库，这样测试和原数据库将分离互补干扰。下面就将说说laravel框架如何连接多个数据库。</p>
<h4 id="首先，在数据库配置文件app-config-database-php中创建新数据库"><a href="#首先，在数据库配置文件app-config-database-php中创建新数据库" class="headerlink" title="首先，在数据库配置文件app/config/database.php中创建新数据库"></a>首先，在数据库配置文件<code>app/config/database.php</code>中创建新数据库</h4><p>这里我新建的数据库为<code>mysql_testing</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">return</span> [</div><div class="line"></div><div class="line"> <span class="string">'default'</span> =&gt; env(<span class="string">'DB_CONNECTION'</span>, <span class="string">'mysql'</span>),</div><div class="line"> </div><div class="line"> <span class="string">'connections'</span> =&gt; [</div><div class="line"> </div><div class="line">    <span class="string">'mysql'</span> =&gt; [</div><div class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'mysql'</span>,</div><div class="line">            <span class="string">'host'</span> =&gt; env(<span class="string">'DB_HOST'</span>, <span class="string">'127.0.0.1'</span>),</div><div class="line">            <span class="string">'port'</span> =&gt; env(<span class="string">'DB_PORT'</span>, <span class="string">'3306'</span>),</div><div class="line">            <span class="string">'database'</span> =&gt; env(<span class="string">'DB_DATABASE'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'username'</span> =&gt; env(<span class="string">'DB_USERNAME'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'password'</span> =&gt; env(<span class="string">'DB_PASSWORD'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'unix_socket'</span> =&gt; env(<span class="string">'DB_SOCKET'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf8mb4'</span>,</div><div class="line">            <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</div><div class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">''</span>,</div><div class="line">            <span class="string">'strict'</span> =&gt; <span class="keyword">true</span>,</div><div class="line">            <span class="string">'engine'</span> =&gt; <span class="keyword">null</span>,</div><div class="line">        ],</div><div class="line"></div><div class="line">    <span class="string">'mysql_testing'</span> =&gt; [</div><div class="line">            <span class="string">'driver'</span> =&gt; <span class="string">'mysql'</span>,</div><div class="line">            <span class="string">'host'</span> =&gt; env(<span class="string">'DB_HOST_TEST'</span>, <span class="string">'127.0.0.1'</span>),</div><div class="line">            <span class="string">'port'</span> =&gt; env(<span class="string">'DB_PORT_TEST'</span>, <span class="string">'3306'</span>),</div><div class="line">            <span class="string">'database'</span> =&gt; env(<span class="string">'DB_DATABASE_TEST'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'username'</span> =&gt; env(<span class="string">'DB_USERNAME_TEST'</span>, <span class="string">'forge'</span>),</div><div class="line">            <span class="string">'password'</span> =&gt; env(<span class="string">'DB_PASSWORD_TEST'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'unix_socket'</span> =&gt; env(<span class="string">'DB_SOCKET'</span>, <span class="string">''</span>),</div><div class="line">            <span class="string">'charset'</span> =&gt; <span class="string">'utf8mb4'</span>,</div><div class="line">            <span class="string">'collation'</span> =&gt; <span class="string">'utf8mb4_unicode_ci'</span>,</div><div class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">''</span>,</div><div class="line">            <span class="string">'strict'</span> =&gt; <span class="keyword">true</span>,</div><div class="line">            <span class="string">'engine'</span> =&gt; <span class="keyword">null</span>,</div><div class="line">        ],</div><div class="line">     ],</div><div class="line"> ];</div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/23/Laravel如何连接多个数据库？/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/17/PHP流程控制/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/17/PHP流程控制/" itemprop="url">
                  PHP流程控制
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-17T14:36:19+08:00">
                2017-05-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/17/PHP流程控制/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/17/PHP流程控制/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.control-structures.php">PHP手册</a></p>
</blockquote>
<h3 id="if"><a href="#if" class="headerlink" title="if"></a>if</h3><p>if 结构是很多语言包括 PHP 在内最重要的特性之一，它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似：</p>
<p>&lt;?php<br>if (expr)<br>  statement<br>?&gt;<br>如同在表达式一章中定义的，expr 按照布尔求值。如果 expr 的值为 TRUE，PHP 将执行 statement，如果值为 FALSE ——将忽略 statement。</p>
<p>如果 $a 大于 $b，则以下例子将显示 a is bigger than b：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($a &gt; $b)</div><div class="line">  <span class="keyword">echo</span> <span class="string">"a is bigger than b"</span>;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="elseif-else-if"><a href="#elseif-else-if" class="headerlink" title="elseif/else if"></a>elseif/else if</h3><p>elseif，和此名称暗示的一样，是 if 和 else 的组合。和 else 一样，它延伸了 if 语句，可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和 else 不一样的是，它仅在 elseif 的条件表达式值为 TRUE 时执行语句。例如以下代码将根据条件分别显示 a is bigger than b，a equal to b 或者 a is smaller than b：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($a &gt; $b) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is bigger than b"</span>;</div><div class="line">&#125; <span class="keyword">elseif</span> ($a == $b) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is equal to b"</span>;</div><div class="line">&#125; <span class="keyword">else</span> &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is smaller than b"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>在同一个 if 语句中可以有多个 elseif 部分，其中第一个表达式值为 TRUE（如果有的话）的 elseif 部分将会执行。在 PHP 中，也可以写成“else if”（两个单词），它和“elseif”（一个单词）的行为完全一样。句法分析的含义有少许区别（如果你熟悉 C 语言的话，与之行为相同），但是底线是两者会产生完全一样的行为。</p>
<p>elseif 的语句仅在之前的 if 和所有之前 elseif 的表达式值为 FALSE，并且当前的 elseif 表达式值为 TRUE 时执行。<br>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/17/PHP流程控制/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/16/PHP运算符/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/16/PHP运算符/" itemprop="url">
                  PHP运算符
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-16T18:14:25+08:00">
                2017-05-16
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/16/PHP运算符/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/16/PHP运算符/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.operators.php">PHP手册</a></p>
</blockquote>
<p>运算符是可以通过给出的一或多个值（用编程行话来说，表达式）来产生另一个值（因而整个结构成为一个表达式）的东西。</p>
<p>运算符可按照其能接受几个值来分组。一元运算符只能接受一个值，例如 !（逻辑取反运算符）或 ++（递增运算符）。 二元运算符可接受两个值，例如熟悉的算术运算符 +（加）和 -（减），大多数 PHP 运算符都是这种。最后是唯一的三元运算符 ? :，可接受三个值；通常就简单称之为“三元运算符”（尽管称之为条件运算符可能更合适）。</p>
<h3 id="运算符优先级"><a href="#运算符优先级" class="headerlink" title="运算符优先级"></a>运算符优先级</h3><p>运算符优先级指定了两个表达式绑定得有多“紧密”。例如，表达式 1 + 5 <em> 3 的结果是 16 而不是 18 是因为乘号（“</em>”）的优先级比加号（“+”）高。必要时可以用括号来强制改变优先级。例如：(1 + 5) * 3 的值为 18。</p>
<p>如果运算符优先级相同，那运算符的结合方向决定了该如何运算。例如，”-“是左联的，那么 1 - 2 - 3 就等同于 (1 - 2) - 3 并且结果是 -4. 另外一方面，”=”是右联的，所以 $a = $b = $c 等同于 $a = ($b = $c)。</p>
<p>没有结合的相同优先级的运算符不能连在一起使用，例如 1 &lt; 2 &gt; 1 在PHP是不合法的。但另外一方面表达式 1 &lt;= 1 == 1 是合法的, 因为 == 的优先级低于 &lt;=。</p>
<p>括号的使用，哪怕在不是必要的场合下，通过括号的配对来明确标明运算顺序，而非靠运算符优先级和结合性来决定，通常能够增加代码的可读性。</p>
<p>运算符优先级<br><img src="/uploads/运算符优先级一.jpg" alt="运算符优先级1"><br><img src="/uploads/运算符优先级二.jpg" alt="运算符优先级2"></p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/16/PHP运算符/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/16/PHP变量和常量/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/16/PHP变量和常量/" itemprop="url">
                  PHP变量和常量
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-16T14:17:49+08:00">
                2017-05-16
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/16/PHP变量和常量/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/16/PHP变量和常量/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.variables.php">PHP手册</a></p>
</blockquote>
<h1 id="PHP变量"><a href="#PHP变量" class="headerlink" title="PHP变量"></a>PHP变量</h1><p>PHP 中的变量用一个美元符号后面跟变量名来表示。<strong>变量名是区分大小写的</strong>。</p>
<p>变量名与 PHP 中其它的标签一样遵循相同的规则。一个有效的变量名由字母或者下划线开头，后面跟上任意数量的字母，数字，或者下划线。按照正常的正则表达式，它将被表述为：<code>&#39;[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*&#39;</code>。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Note: <span class="keyword">$this</span> 是一个特殊的变量，它不能被赋值。</div></pre></td></tr></table></figure>
<p>变量默认总是<strong>传值赋值</strong>。那也就是说，当将一个表达式的值赋予一个变量时，整个原始表达式的值被赋值到目标变量。这意味着，例如，当一个变量的值赋予另外一个变量时，改变其中一个变量的值，将不会影响到另外一个变量。</p>
<p>PHP 也提供了另外一种方式给变量赋值：引用赋值。这意味着新的变量简单的引用（换言之，“成为其别名” 或者 “指向”）了原始变量。改动新的变量将影响到原始变量，反之亦然。</p>
<p>使用引用赋值，简单地将一个<code>&amp;</code>符号加到将要赋值的变量前（源变量）。例如，下列代码片断将输出“My name is Bob”两次：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$foo = <span class="string">'Bob'</span>;              <span class="comment">// 将 'Bob' 赋给 $foo</span></div><div class="line">$bar = &amp;$foo;              <span class="comment">// 通过 $bar 引用 $foo</span></div><div class="line">$bar = <span class="string">"My name is $bar"</span>;  <span class="comment">// 修改 $bar 变量</span></div><div class="line"><span class="keyword">echo</span> $bar;</div><div class="line"><span class="keyword">echo</span> $foo;                 <span class="comment">// $foo 的值也被修改</span></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>有一点重要事项必须指出，那就是<strong>只有有名字的变量才可以引用赋值</strong>。</p>
<p>虽然在 PHP 中并不需要初始化变量，但对变量进行初始化是个好习惯。未初始化的变量具有其类型的默认值 - 布尔类型的变量默认值是 FALSE，整形和浮点型变量默认值是零，字符串型变量（例如用于 echo 中）默认值是空字符串以及数组变量的默认值是空数组。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/16/PHP变量和常量/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/15/PHP类型-下篇/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/15/PHP类型-下篇/" itemprop="url">
                  PHP类型(下篇)
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-15T18:58:49+08:00">
                2017-05-15
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/15/PHP类型-下篇/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/15/PHP类型-下篇/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.types.php">PHP手册</a></p>
</blockquote>
<h1 id="PHP类型-下篇"><a href="#PHP类型-下篇" class="headerlink" title="PHP类型(下篇)"></a>PHP类型(下篇)</h1><h3 id="Array-数组"><a href="#Array-数组" class="headerlink" title="Array 数组"></a>Array 数组</h3><p>PHP 中的数组实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。此类型在很多方面做了优化，因此可以把它当成真正的数组，或列表（向量），散列表（是映射的一种实现），字典，集合，栈，队列以及更多可能性。由于数组元素的值也可以是另一个数组，树形结构和多维数组也是允许的</p>
<h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><h5 id="定义数组-array"><a href="#定义数组-array" class="headerlink" title="定义数组 array()"></a>定义数组 array()</h5><figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$array = <span class="keyword">array</span>(</div><div class="line">    <span class="string">"foo"</span> =&gt; <span class="string">"bar"</span>,</div><div class="line">    <span class="string">"bar"</span> =&gt; <span class="string">"foo"</span>,</div><div class="line">);</div><div class="line"></div><div class="line"><span class="comment">// 自 PHP 5.4 起</span></div><div class="line">$array = [</div><div class="line">    <span class="string">"foo"</span> =&gt; <span class="string">"bar"</span>,</div><div class="line">    <span class="string">"bar"</span> =&gt; <span class="string">"foo"</span>,</div><div class="line">];</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p><code>key</code> 可以是 <code>integer</code> 或者 <code>string</code>。<code>value</code> 可以是任意类型。</p>
<p>此外 <code>key</code> 会有如下的强制转换：</p>
<ul>
<li>包含有合法整型值的字符串会被转换为整型。例如键名<code>&quot;8&quot;</code> 实际会被储存为 <code>8</code>。但是 <code>&quot;08&quot;</code> 则不会强制转换，因为其不是一个合法的十进制数值。</li>
<li>浮点数也会被转换为整型，意味着其小数部分会被舍去。例如键名<code>8.7</code> 实际会被储存为 <code>8</code>。</li>
<li>布尔值也会被转换成整型。即键名 <code>true</code> 实际会被储存为 <code>1</code> 而键名 <code>false</code> 会被储存为 <code>0</code>。</li>
<li><code>Null</code> 会被转换为空字符串，即键名 <code>null</code> 实际会被储存为 <code>&quot;&quot;</code>。</li>
<li>数组和对象不能被用为键名。坚持这么做会导致警告：<code>Illegal offset type</code>。</li>
</ul>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/15/PHP类型-下篇/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/15/PHP类型-上篇/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/05/15/PHP类型-上篇/" itemprop="url">
                  PHP类型(上篇)
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-15T18:53:07+08:00">
                2017-05-15
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/15/PHP类型-上篇/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/15/PHP类型-上篇/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.types.php">PHP手册</a></p>
</blockquote>
<h1 id="PHP类型-上篇"><a href="#PHP类型-上篇" class="headerlink" title="PHP类型(上篇)"></a>PHP类型(上篇)</h1><p>PHP 支持 9 种原始数据类型。</p>
<p>四种标量类型：</p>
<ul>
<li>boolean(布尔型)</li>
<li>integer(整型)</li>
<li>float(浮点型，也称double)</li>
<li>string(字符串)</li>
</ul>
<p>三种复合类型：</p>
<ul>
<li>array(数组)</li>
<li>object(对象)</li>
<li>callable(可调用的)</li>
</ul>
<p>最后是两种特殊类型：</p>
<ul>
<li>resource(资源)</li>
<li>Null(无类型)</li>
</ul>
<h3 id="布尔类型Boolean"><a href="#布尔类型Boolean" class="headerlink" title="布尔类型Boolean"></a>布尔类型Boolean</h3><h4 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h4><p>要指定一个布尔值，<strong>True</strong>或<strong>False</strong>，不区分大小写。<br>简介写法：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="comment">// == 是一个操作符，它检测两个变量是否相等，并返回一个布尔值</span></div><div class="line"><span class="keyword">if</span> ($action == <span class="string">"show_version"</span>) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"The version is 1.23"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 这样做是不必要的...</span></div><div class="line"><span class="keyword">if</span> ($show_separators == <span class="keyword">TRUE</span>) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"&lt;hr&gt;\n"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// ...因为可以使用下面这种简单的方式：</span></div><div class="line"><span class="keyword">if</span> ($show_separators) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"&lt;hr&gt;\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h4 id="转换为布尔值"><a href="#转换为布尔值" class="headerlink" title="转换为布尔值"></a>转换为布尔值</h4><p>要将一个值转换成boolean时，用（bool）或者（boolean）来强制转换。但通常不用强制转换，<strong>当运算符，函数流程控制结构需要boolean参数时，该值会被自动转换。</strong></p>
<p>转换为boolean时，以下值被认为是False：</p>
<ul>
<li>布尔值 FALSE 本身</li>
<li>整型值 0（零）</li>
<li>浮点型值 0.0（零）</li>
<li>空字符串，以及字符串 “0”</li>
<li>不包括任何元素的数组</li>
<li>特殊类型 NULL（包括尚未赋值的变量）</li>
<li>从空标记生成的 SimpleXML 对象</li>
</ul>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">var_dump((bool) <span class="string">""</span>);        <span class="comment">// bool(false)</span></div><div class="line">var_dump((bool) <span class="number">1</span>);         <span class="comment">// bool(true)</span></div><div class="line">var_dump((bool) <span class="number">-2</span>);        <span class="comment">// bool(true)</span></div><div class="line">var_dump((bool) <span class="string">"foo"</span>);     <span class="comment">// bool(true)</span></div><div class="line">var_dump((bool) <span class="number">2.3e5</span>);     <span class="comment">// bool(true)</span></div><div class="line">var_dump((bool) <span class="keyword">array</span>(<span class="number">12</span>)); <span class="comment">// bool(true)</span></div><div class="line">var_dump((bool) <span class="keyword">array</span>());   <span class="comment">// bool(false)</span></div><div class="line">var_dump((bool) <span class="string">"false"</span>);   <span class="comment">// bool(true)</span></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="comment">// Consider that the 0 could by any parameters including itself</span></div><div class="line">var_dump(<span class="number">0</span> == <span class="number">1</span>); <span class="comment">// false</span></div><div class="line">var_dump(<span class="number">0</span> == (bool)<span class="string">'all'</span>); <span class="comment">// false</span></div><div class="line">var_dump(<span class="number">0</span> == <span class="string">'all'</span>); <span class="comment">// TRUE, take care</span></div><div class="line">var_dump(<span class="number">0</span> === <span class="string">'all'</span>); <span class="comment">// false</span></div><div class="line"></div><div class="line"><span class="comment">// To avoid this behavior, you need to cast your parameter as string like that :</span></div><div class="line">var_dump((string)<span class="number">0</span> == <span class="string">'all'</span>); <span class="comment">// false</span></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/05/15/PHP类型-上篇/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/20/Laravel-Notifications实现站内信通知/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/04/20/Laravel-Notifications实现站内信通知/" itemprop="url">
                  Laravel Notifications实现站内信通知
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-20T18:47:53+08:00">
                2017-04-20
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/04/20/Laravel-Notifications实现站内信通知/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/04/20/Laravel-Notifications实现站内信通知/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自于JellyBool的视频，地址: <a href="https://www.laravist.com/series/build-a-zhihu-website-with-laravel/episodes/21">Laravel 实战开发知乎：站内信通知</a></p>
</blockquote>
<h4 id="很早就听说过站内信通知，一直以为实现站内通信功能应该会很困难，没有一点思路，直到遇见了Laravist…"><a href="#很早就听说过站内信通知，一直以为实现站内通信功能应该会很困难，没有一点思路，直到遇见了Laravist…" class="headerlink" title="很早就听说过站内信通知，一直以为实现站内通信功能应该会很困难，没有一点思路，直到遇见了Laravist…"></a>很早就听说过站内信通知，一直以为实现站内通信功能应该会很困难，没有一点思路，直到遇见了Laravist…</h4><p>这次我们将用<strong>laravel 5.3</strong>实现关注用户消息提醒，为了更好的理解下列内容，建议去看看官方文档（<a href="http://d.laravel-china.org/docs/5.3/notifications">laravel notifications</a>）。</p>
<p>laravel支持多种频道发送通知，下面我将使用数据库通知。</p>
<h4 id="创建Notification文件"><a href="#创建Notification文件" class="headerlink" title="创建Notification文件"></a>创建Notification文件</h4><p>因此这次是做关注用户消息提醒，使用下列命令创建</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">php artisan make:notification NewUserFollowNotification</div></pre></td></tr></table></figure>
<h4 id="创建notification迁移表"><a href="#创建notification迁移表" class="headerlink" title="创建notification迁移表"></a>创建notification迁移表</h4><p>数据库通知频道在一张数据表里存储通知信息。这张表包含了比如通知类型、JSON 格式数据等描述通知的信息。</p>
<p>你可以查询这张表的内容在应用界面上展示通知。但是在这之前，你需要先创建一个数据表来保存通知。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">php artisan notifications:table</div><div class="line"></div><div class="line">php artisan migrate</div></pre></td></tr></table></figure>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/04/20/Laravel-Notifications实现站内信通知/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/18/Laravel-前端API-Token认证/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/04/18/Laravel-前端API-Token认证/" itemprop="url">
                  Laravel 前端API Token认证
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-18T15:34:45+08:00">
                2017-04-18
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/04/18/Laravel-前端API-Token认证/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/04/18/Laravel-前端API-Token认证/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          <blockquote>
<p>本文内容来自于JellyBool的视频，地址: <a href="https://www.laravist.com/series/build-a-zhihu-website-with-laravel/episodes/21">Laravel 实战开发知乎：前后端分离 API token 认证 </a></p>
</blockquote>
<h1 id="Laravel-前端API-Token认证"><a href="#Laravel-前端API-Token认证" class="headerlink" title="Laravel 前端API Token认证"></a>Laravel 前端API Token认证</h1><p>Laravel 的 API 认证提供了两种方式，一是使用 <code>passport</code> package，二是使用 <code>token</code>来进行认证，今天使用的是<code>token</code>方式来认证。</p>
<h3 id="查看Laravel源码TokenGuard-php可以看到"><a href="#查看Laravel源码TokenGuard-php可以看到" class="headerlink" title="查看Laravel源码TokenGuard.php可以看到"></a>查看Laravel源码<code>TokenGuard.php</code>可以看到</h3>   <figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">$this</span>-&gt;inputKey = <span class="string">'api_token'</span>;</div><div class="line"><span class="keyword">$this</span>-&gt;storageKey = <span class="string">'api_token'</span>;</div></pre></td></tr></table></figure>
<p>   laravel规定<code>inputKey</code>为<code>api_token</code>,因此我们创建为用户创建的字段名也得为<code>api_token</code>.</p>
<h3 id="为用户表添加字段api-token"><a href="#为用户表添加字段api-token" class="headerlink" title="为用户表添加字段api_token"></a>为用户表添加字段<code>api_token</code></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt; php artisan make:migration add_api_token_to_users --table=users</div></pre></td></tr></table></figure>
<p>在<code>add_api_token_to_users.php</code>中修改如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"> <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">up</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    Schema::table(<span class="string">'users'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(Blueprint $table)</span> </span>&#123;</div><div class="line">        $table-&gt;string(<span class="string">'api_token'</span>,<span class="number">64</span>)-&gt;unique();</div><div class="line">    &#125;);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">down</span><span class="params">()</span></div><div class="line"></span>&#123;</div><div class="line">    Schema::table(<span class="string">'users'</span>, <span class="function"><span class="keyword">function</span> <span class="params">(Blueprint $table)</span> </span>&#123;</div><div class="line">        $table-&gt;dropColumn([<span class="string">'api_token'</span>]);</div><div class="line">    &#125;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&gt; php artisan migrate</div></pre></td></tr></table></figure>
<p>进行上面操作后，在user表中可以看到已经多了一个<code>api_token</code>字段。</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/2017/04/18/Laravel-前端API-Token认证/#more" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/17/Laravel-ide-helper-高效的IDE-智能提示插件/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/04/17/Laravel-ide-helper-高效的IDE-智能提示插件/" itemprop="url">
                  Laravel-ide-helper 高效的IDE 智能提示插件
                </a>
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-17T14:19:07+08:00">
                2017-04-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/04/17/Laravel-ide-helper-高效的IDE-智能提示插件/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/04/17/Laravel-ide-helper-高效的IDE-智能提示插件/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>github地址:<a href="https://github.com/barryvdh/laravel-ide-helper" target="_blank" rel="external">Laravel-ide-helper</a></p>
<h3 id="how-to-use-in-project："><a href="#how-to-use-in-project：" class="headerlink" title="how to use in project："></a>how to use in project：</h3><ol>
<li><p>首先在项目中引入：</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">composer require barryvdh/laravel-ide-helper</div></pre></td></tr></table></figure>
</li>
<li><p>安装完成后，在 <code>config/app.php</code> 添加以下内容到 <code>providers</code> 数组。</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,</div></pre></td></tr></table></figure>
</li>
<li><p>运行下面的命令生成代码对应文档：</p>
  <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">php artisan ide-helper:generate</div></pre></td></tr></table></figure>
</li>
<li><p>将扩展包生成的相应代码结构文件，放入 <code>.gitignore</code>中</p>
 <figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">.idea</div><div class="line">_ide_helper.php</div><div class="line">_ide_helper_models.php</div><div class="line">.phpstorm.meta.php</div></pre></td></tr></table></figure>
</li>
</ol>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>


          
          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://gasbylei.github.io/uploads/avatar.png"
               alt="Gasbylei" />
          <p class="site-author-name" itemprop="name">Gasbylei</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/Gasbylei" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gasbylei</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'gatsbylei';
      var disqus_identifier = 'index.html';

      var disqus_title = "";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
